﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class someday : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            int num5;
            DateTime time3;
            DateTime time4;
            DateTime time5;
            string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : base.User.Identity.Name;
            string str2 = (Request.QueryString["somedate"] != null) ? Request.QueryString["somedate"].ToString() : DateTime.Now.ToString("yyyy-MM-dd");
            this.litSomeDate.Text = Convert.ToDateTime(str2).ToString("D");
            int num = 30;
            int num2 = 10;
            int num3 = 10;
            int num4 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num = dao.DutyIntervalBefore1;
            num2 = dao.DutyIntervalAfter1;
            num3 = dao.DutyIntervalBefore2;
            num4 = dao.DutyIntervalAfter2;
            DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
            this.litDutyName.Text = userConfig.Rows[0]["DutyName"].ToString();
            DataTable table2 = new DataTable();
            table2.Columns.Add("No", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("DutyId", Type.GetType("System.Int32"));
            table2.Columns.Add("RegisterTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("RegisterFlag", Type.GetType("System.Int32"));
            table2.Columns.Add("RegisterIp", Type.GetType("System.String"));
            table2.Columns.Add("Remark", Type.GetType("System.String"));
            if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 1, num5, time3, time4, time5 });
            }
            if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 2, num5, time3, time4, time5 });
            }
            if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 3, num5, time3, time4, time5 });
            }
            if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 4, num5, time3, time4, time5 });
            }
            if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 5, num5, time3, time4, time5 });
            }
            if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                time3 = Convert.ToDateTime(str2 + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                if (num5 == 1)
                {
                    time4 = time3.AddMinutes((double)-num);
                    time5 = time3.AddMinutes((double)num2);
                }
                else
                {
                    time4 = time3.AddMinutes((double)-num3);
                    time5 = time3.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 6, num5, time3, time4, time5 });
            }
            bool flag = false;
            string commandText = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + Convert.ToDateTime(str2).ToString("yyyy-MM-dd") + "' AND '" + Convert.ToDateTime(str2).ToString("yyyy-MM-dd") + "' <= [EndDate]";
            if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, commandText) != null)
            {
                flag = true;
            }
            commandText = "SELECT * FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + Convert.ToDateTime(str2).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + Convert.ToDateTime(str2).ToString("yyyy-MM-dd") + " 23:59:59'";
            DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
            for (int i = 0; i < table2.Rows.Count; i++)
            {
                DataRow[] rowArray = table3.Select("RegisterType = " + table2.Rows[i]["DutyType"].ToString() + " AND RegisterTime >='" + table2.Rows[i]["BeginTime"].ToString() + "' AND RegisterTime <='" + table2.Rows[i]["EndTime"].ToString() + "'");
                if (rowArray.Length > 0)
                {
                    table2.Rows[i]["DutyId"] = Convert.ToInt32(rowArray[0]["DutyId"].ToString());
                    table2.Rows[i]["RegisterTime"] = Convert.ToDateTime(rowArray[0]["RegisterTime"].ToString());
                    table2.Rows[i]["RegisterFlag"] = Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString());
                    table2.Rows[i]["RegisterIp"] = rowArray[0]["RegisterIp"].ToString();
                    if (rowArray[0]["Remark"] != DBNull.Value)
                    {
                        table2.Rows[i]["Remark"] = rowArray[0]["Remark"].ToString();
                    }
                }
                else
                {
                    table2.Rows[i]["RegisterFlag"] = 0;
                    if (flag)
                    {
                        table2.Rows[i]["DutyId"] = -4;
                    }
                    else
                    {
                        commandText = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + table2.Rows[i]["DutyTime"].ToString() + "' AND [OutTime2]>='" + table2.Rows[i]["DutyTime"].ToString() + "'";
                        DataTable table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                        if (table4.Rows.Count > 0)
                        {
                            table2.Rows[i]["DutyId"] = -1;
                        }
                        else
                        {
                            commandText = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + table2.Rows[i]["DutyTime"].ToString() + "' AND [LeaveDate2]>='" + table2.Rows[i]["DutyTime"].ToString() + "'";
                            DataTable table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                            if (table5.Rows.Count > 0)
                            {
                                table2.Rows[i]["DutyId"] = -2;
                            }
                            else
                            {
                                commandText = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + Convert.ToDateTime(table2.Rows[i]["DutyTime"].ToString()).ToString("yyyy-MM-dd") + "' AND [EvectionDate1]>='" + Convert.ToDateTime(table2.Rows[i]["DutyTime"].ToString()).ToString("yyyy-MM-dd") + "'";
                                DataTable table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                if (table6.Rows.Count > 0)
                                {
                                    table2.Rows[i]["DutyId"] = -3;
                                }
                            }
                        }
                    }
                }
            }
            this.rptMy.DataSource = table2;
            this.rptMy.DataBind();
        } 

    }
}
